<?php

include_once('ligature/user.class.php');

class Ligature_User_model extends Model{
    
    function Ligature_User_model() {
        parent::Model();
    }
    
    /**
     *  Create the user in the database, if necessary.
     *  Set appropriate session variables.
     *  @param object $tokens Access token from Twitter
     *  @param object $userInfo User information from Twitter verify_credentials request
     */
    function login($token, $userInfo) {
        
        $sql = 'INSERT INTO ligature_user (twitterId, name, twitterScreenName, profileImageUrl, dateCreated, lastLogin) ' .
            'VALUES (?, ?, ?, ?, ?, ?) ' .
            'ON DUPLICATE KEY UPDATE name = ?, twitterScreenName = ?, profileImageUrl = ?, lastLogin = ?';
        
        $currentTime = time();    
        
        $this->db->query($sql, array($userInfo->id, $userInfo->name, $userInfo->screen_name, $userInfo->profile_image_url, $currentTime, $currentTime, $userInfo->name, $userInfo->screen_name, $userInfo->profile_image_url, $currentTime));

        $user = $this->readUserByTwitterId($userInfo->id);

        log_message('debug', 'User ' . $userInfo->name . ' has been created or updated.');
        
        $this->session->set_userdata('oauth_token', $token->oauth_token);
        $this->session->set_userdata('oauth_token_secret', $token->oauth_token_secret);
        $this->session->set_userdata('current_user_id', $user->getUserId());
        $this->session->set_userdata('current_user_twitter_id', $userInfo->id);
        $this->session->set_userdata('current_user_name', $userInfo->name);
        $this->session->set_userdata('current_user_screen_name', $userInfo->screen_name);
        $this->session->set_userdata('is_admin', in_array($userInfo->screen_name, $this->config->item('admins', 'ligature')));
        
        log_message('debug', 'User ' . $userInfo->name . ' has been logged in.');
    }
    
    function logout() {
        $this->session->sess_destroy();
    }

    /**
     *  Retrieve a User from the database using its identifier.
     *  @param integer $id User's unique identifier
     *  @return User
     */
    function readUser($id) {
        
        $query = $this->db->get_where('ligature_user', array('id' => $id), 1);
        $row = $query->result();
        
        return new User($row->id, $row->name, $row->twitterScreenName, $row->profileImageUrl);
        
    }
    
    function readUserByTwitterId($twitterId) {
        
        $query = $this->db->get_where('ligature_user', array('twitterId' => $twitterId));
        $row = $query->row();
        
        return new User($row->id, $row->name, $row->twitterScreenName, $row->profileImageUrl);
        
    }
    
    /**
     *  Retrieve an array of User objects.
     *  @param integer $page Which batch of results to display
     *  @param integer $perPage Number of User objects to return in batch, 0 for all
     *  @return array
     */
    function listUser($page = 1, $perPage = 0) {
        // TODO: Retrieve all Users from data source
        // TODO: Create and populate User instances
        return array();
    }
    
    /**
     *  Retrieve an array of User objects which match the provided
     *  search terms.
     *  @param string $searchTerms Search terms to match
     *  @return array
     */
    function searchUser($searchTerms) {
        // TODO: Retrieve all matching Users from data source
        // TODO: Create and populate User instances
        return array();        
    }
    
    /**
     *  Update an existing User in the database. 
     *  @param User $user The updated object.
     *  @return boolean Returns true if update successful.
     */
    function updateUser($user) {
        // TODO: Update User in data source
        return true;
    }
    
    /**
     *  Delete an User from the database.
     *  @param mixed $id Unique identifier of User to delete
     *  @return boolean Returns true if delete successful.
     */
    function deleteUser($id) {
        // TODO: Remove User from data source
        return true;
    }
    
}

/* clickframes::version=411236160::clickframes */
?>